package com.android.huimeiqupin.fragment.group;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.huimeiqupin.R;
import com.android.huimeiqupin.activity.MainTab;
import com.android.huimeiqupin.adapt.MainListSortBrandAdapt;
import com.android.huimeiqupin.adapt.MainNationAdapt;
import com.android.huimeiqupin.cache.DiskLruCacheHelper;
import com.android.huimeiqupin.fragment.BaseLazyFragment;
import com.android.huimeiqupin.model.group.SortBrandData;
import com.android.huimeiqupin.model.group.SortBrandDataCallback;
import com.android.huimeiqupin.model.group.SortNationData;
import com.android.huimeiqupin.model.group.SortNationDataCallback;
import com.android.huimeiqupin.utils.ConstantsUrl;
import com.android.huimeiqupin.utils.HttpUtils;
import com.android.huimeiqupin.utils.ObjectUtils;
import com.android.huimeiqupin.view.group.VRefreshLayout;
import com.android.huimeiqupin.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 海淘  大牌 分类展示
 * ---------------------------------------
 */
public class Fragment_group_BrandNation_Sort extends BaseLazyFragment {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    VRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;

    private static final String ARG_PARAM1 = "param1";
    private int type;
    private View rootView;
    private boolean networkConnected;
    private LayoutInflater inflater;
    private FragmentActivity mcontext;
    private DiskLruCacheHelper helper;
    private MainListSortBrandAdapt adapter;
    private MainNationAdapt adapter_nation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(ARG_PARAM1);
        }
    }

    public static Fragment_group_BrandNation_Sort newInstance(int type) {
        Fragment_group_BrandNation_Sort fragment = new Fragment_group_BrandNation_Sort();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initPrepare() {
        switch (type) {
            case 0:
                adapter = new MainListSortBrandAdapt(R.layout.item_list_sort_dapai, null);
                adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                mRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                        Intent intent = new Intent(mcontext, MainTab.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, position);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 0);
                        startActivity(intent);
                    }
                });
                break;
            case 1:
                View head = inflater.inflate(R.layout.item_list_sort_dapai, null, false);
                head.findViewById(R.id.ll_img).setVisibility(View.GONE);
                head.findViewById(R.id.ll_nation_logo).setVisibility(View.VISIBLE);
                adapter_nation = new MainNationAdapt(R.layout.item_list_sort_dapai, null);
                adapter_nation.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                adapter_nation.addHeaderView(head);
                mRecyclerView.setAdapter(adapter_nation);
                adapter_nation.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                        Intent intent = new Intent(mcontext, MainTab.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, position);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 1);
                        startActivity(intent);
                    }
                });
                break;
        }
        //刷新
        Refresh();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (type == 0) {
            GetListData_Brand();
        }
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        if (type == 1) {
            GetListData_Nation();
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        rootView = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        initUI();
        return rootView;
    }

    private void initUI() {
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mainTopBg.setVisibility(View.GONE);
        rootView.findViewById(R.id.top).setVisibility(View.INVISIBLE);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mcontext);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }

    private void Refresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.setBackgroundColor(Color.TRANSPARENT);
            mRefreshLayout.setAutoRefreshDuration(400);
            mRefreshLayout.setRatioOfHeaderHeightToReach(1.5f);
            mRefreshLayout.addOnRefreshListener(new VRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                    if (networkConnected) {
                        mRefreshLayout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                switch (type) {
                                    case 0:
                                        GetListData_Brand();
                                        break;
                                    case 1:
                                        GetListData_Nation();
                                        break;
                                }
                                mRefreshLayout.refreshComplete();
                            }
                        }, 1000);
                    } else {
                        ObjectUtils.toast(mcontext, "网络连接出现异常");
                    }
                }
            });
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        switch (type) {
            case 0:
                GetListData_Brand();
                break;
            case 1:
                GetListData_Nation();
                break;
        }
    }

    //超值大牌
    private void GetListData_Brand() {
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MAINLIST_DAPAI;
            HttpUtils.get(address, null, new SortBrandDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SortBrandData response, int id) {
                    if(response == null){
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_DAPAI, response.toString());
                        ArrayList<SortBrandData.DataBean.ListBean> listdata = new ArrayList<>();
                        SortBrandData.DataBean data = response.getData();
                        if (data == null || !ObjectUtils.listHasData(data.getList())) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        listdata.addAll(data.getList());
                        adapter.setNewData(listdata);
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    //海淘
    private void GetListData_Nation() {
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.NATIONLISTDATA;
            HttpUtils.get(address, null, new SortNationDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SortNationData response, int id) {
                    if(response == null){
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_NATION, response.toString());
                        ArrayList<SortNationData.Nationdata> listleftdata = new ArrayList<>();
                        List<SortNationData.Nationdata> data = response.getData();
                        if (data == null) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        listleftdata.addAll(data);
                        adapter_nation.setNewData(listleftdata);
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }
}
